package com.yxd.util;

import com.yxd.logging.SysLogger;
import com.yxd.server.Request;
import com.yxd.vo.RequestLine;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

/**
 * Created by Administrator on 2016/6/16.
 */
public class SocketInputStream extends InputStream {
    private final InputStream inputStream;

    public SocketInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public RequestLine getRequestLine() {
        StringBuilder stringBuilder = new StringBuilder();
        RequestLine requestLine = new RequestLine();
        try {
            int iCh = read();
            while (iCh != -1) {
                if (((char) iCh) == '\r' || ((char) iCh) == '\n') {
                    break;
                }
                stringBuilder.append((char) iCh);
                iCh = read();
            }
            String string = stringBuilder.toString();
            int idx1 = string.indexOf(" ");
            if (idx1 >= 0) {
                String method = string.substring(0, idx1);
                int idx2 = string.indexOf(" ", idx1 + 1);
                if (idx2 >= 0) {
                    String uri = string.substring(idx1 + 1, idx2);
                    int idx4 = uri.indexOf("?");
                    if (idx4 >= 0) {
                        uri = uri.substring(0, idx4);
                        requestLine.setQueryString(uri.substring(idx4 + 1));
                    }
                    String version = string.substring(idx2 + 1);
                    int idx3 = version.indexOf("/");
                    if (idx3 >= 0) {
                        requestLine.setProtocal(version.substring(0, idx3));
                        version = version.substring(idx3 + 1);
                    }
                    requestLine.setUri(uri);
                    requestLine.setVersion(version);
                }
                requestLine.setMethod(method);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return requestLine;
    }

    @Override
    public int read() throws IOException {
        return this.inputStream.read();
    }

    public HashMap<String, String> getRequestHeaders() {
        HashMap<String, String> headers = new HashMap<>();
        try {
            int iCh = read();
            while (iCh != -1) {
                char ch = (char) iCh;
                if (ch == '\n') {
                    break;
                }
                iCh = read();
            }
            if (iCh != -1) {
                StringBuilder stringBuilder = new StringBuilder();
                iCh = read();
                String key = null;
                String value;
                boolean isNewLine = true;
                while (iCh != -1) {
                    char ch = (char) iCh;
                    iCh = read();
                    if (ch == '\r') {
                        continue;
                    }
                    if (ch == '\n') {
                        if (key != null) {
                            value = stringBuilder.toString();
                            stringBuilder.delete(0, key.length());
                            headers.put(key, value);
                        }
                        isNewLine = true;
                        continue;
                    }
                    if (isNewLine && ch == ':') {
                        isNewLine = false;
                        key = stringBuilder.toString();
                        stringBuilder.delete(0, key.length());
                    } else {
                        stringBuilder.append(ch);
                    }
                }
                SysLogger.print(headers.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return headers;
    }
}
